export function startPop() {
  window.addEventListener('popstate', (e) => {
    preventBack(e);
  });

  document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') {
      list.length && window.history.back();
    }
  });
}

let list = window.popupStack || (window.popupStack = []);

function preventBack(e) {
  let data = list.pop();
  if (!data) return;
  data.close && data.close();
  e.preventDefault();
  e.stopPropagation();
}

export function pushPopup(data) {
  window.history.pushState(null, null, document.URL); // 添加一个状态到历史记录
  list.push(data);
}

export function closePopup() {
  window.history.back();
}